package com.weetop.aklyscan.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.base.mvp.BasePresenter;
import com.weetop.aklyscan.base.mvp.BaseView;
import com.weetop.aklyscan.bean.PostMessage;
import com.weetop.aklyscan.utils.EventBusUtil;
import com.weetop.aklyscan.utils.widget.MyDialogUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * File descripition:   ftagment 基类
 *
 * @author lp
 * @date 2018/6/19
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
    public View view;
    protected Activity mActivity;
    public Context mContext;
    protected P mPresenter;
    public MyApplication myApplication;
    protected int pageNum = 1;
    protected int pageSize = 15;
    protected boolean isClear = false;

    protected abstract P createPresenter();

    protected QMUITipDialog tipDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isRegisterEventBus()) {
            EventBusUtil.register(this);
        }
        myApplication = (MyApplication) getActivity().getApplication();
        mContext = getActivity();
        mActivity = getActivity();
        mPresenter = createPresenter();
    }

    @Override
    public void showError(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void onDataError() {

    }
    @Override
    public void onError() {

    }

    @Override
    public void onUpFail(String mag) {
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }


    @Override
    public void onErrorCode(BaseModel model) {
        if (model != null) {
            if (model.getCode() == 888) {
                final AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(mActivity);
                normalDialog.setTitle("提示");
                normalDialog.setMessage("账号已在其他地方登陆");
                normalDialog.setPositiveButton("重新登陆", (dialog, which) -> {
                    dialog.dismiss();
                });
                normalDialog.setOnDismissListener(dialog -> {
//                    Intent intent = new Intent(mActivity, LoginActivity.class);
//                    startActivity(intent);
//                    ActivityUtils.finishAllActivitiesExceptNewest();
//                    mActivity.finish();
                });
                normalDialog.show();
            } else {
                if(!StringUtils.isEmpty(model.getMsg()))
                ToastUtils.showShort(model.getMsg());
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.view = null;
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }


    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * [含有Bundle通过Class打开编辑界面]
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(PostMessage event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(PostMessage event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(PostMessage event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(PostMessage event) {

    }

    @Override
    public void showLoading() {
        if (isShowNetLoading())
            showDialog("请稍后");
    }

    @Override
    public void hideLoading() {
        if (isShowNetLoading())
            dissmissDialog();
    }

    /**
     * 接口加载
     *
     * @return
     */
    protected boolean isShowNetLoading() {
        return true;
    }

    protected void showDialog(String str) {
        if (str.equals("")) {
            tipDialog = new QMUITipDialog.Builder(mActivity)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .create();
        } else {
            tipDialog = new QMUITipDialog.Builder(mActivity)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(str)
                    .create();
        }

        tipDialog.show();
    }


    protected void dissmissDialog() {
        if (tipDialog != null)
            tipDialog.dismiss();
    }
}
